package Model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;
import java.util.Vector;

import Messaging.Notification;
import Messaging.NotificationManager;
import Ui.MessagePopUp;

/*
 * @author Roch San Buenaventura
 */
	
public class ChanceCardSpace extends Space implements Serializable 
{
	Vector<Card> Stack_;
	ListIterator<Card> Itr_;
	ArrayList<Player> Players_;
	private static ChanceCardSpace INSTANCE = null;
	public static final int NUMBER = 15;
	
	final static long serialVersionUID = 206;
	
	protected ChanceCardSpace(ArrayList<Player> players) 
	{
		Stack_ = new Vector<Card>(NUMBER);
		
		Stack_.add(new CardTypeOutOfJail());
		Stack_.add(new CardTypeMove("Advance to Illinois Ave.!", 24));
		Stack_.add(new CardTypeWinMoney("Bank pays you dividend of $50!", 50));
		Stack_.add(new CardTypeMove("Advance to Boardwalk!", 39));
		Stack_.add(new CardTypeWinMoney("Your load matures. Collect $150!", 150));
		Stack_.add(new CardTypePayFund("Speeding fine $15!", 15));
		Stack_.add(new CardTypeMove("Go To Jail. Do not pass Go. Do not collect $200!", 10));
		Stack_.add(new CardTypeMove("Advance to Go", 0));
		Stack_.add(new CardTypeWinMoney("You have won a crossword competition! Collect $100!", 100));
		Stack_.add(new CardTypeMove("Go to Reading Railroad!", 5));
		Stack_.add(new CardTypeMove("Advance to St. Charles Place!", 19)); 
		Stack_.add(new CardTypePayFund("Make general repairs on all your property! Pay $100!", 100));
		Stack_.add(new CardTypePayFund("Buy cigarettes for Prof. Mui. Pay $50!", 50)); 
		Stack_.add(new CardTypePayPlayers("You have been elected chairman of the board. Pay each player $50!", 50));
		
		Players_ = players;
		Itr_ = Stack_.listIterator();
	}
	
	public static ChanceCardSpace Instance(ArrayList<Player> players) 
	{
		if (INSTANCE == null) {
			INSTANCE = new ChanceCardSpace(players);
		}
		
		return INSTANCE;
	}
	
	public String toString() 
	{
		return "Chance";
	}
	
	public void landOn(Player p) 
	{
		Card card = drawCard();
		NotificationManager.getInstance().notifyObservers(Notification.SHOW_CARD, card);
		NotificationManager.getInstance().notifyObservers(Notification.MESSAGE_POPUP,  p.getName() + " drew the following Chance card: " + card.getMessage(), false);
		new MessagePopUp(card, p, Players_, "Chance");
	}
	
	public Card drawCard() 
	{
		if (Itr_.hasNext())
			return Itr_.next();
		
		else 
		{
			while(Itr_.hasPrevious()) 
			{
				Itr_.previous();
			}
			return Itr_.next();
		}
	}
	
	public void ShuffleCards() 
	{
		Collections.shuffle(Stack_);
	}
}
